$(function(){
    function register(){
        let $options = {
            type : "POST",
            data : {
                username : `${$(".username").val()}`,
                password : `${$(".psd1").val()}`, 
                rpassword : `${$(".psd2").val()}`, 
                nickname : `${$(".nickname").val()}`,
            },
            contentType : "application/x-www-form-urlencoded",
            dataType : "json"
        };

        let res = $.ajax("http://localhost:8888/users/register",$options);
        res.done( (data) => {
            // console.log($.type(data));jquery中查看数据类型
            
            switch(data.code){
                case 0:
                    failTips(data);
                    break;
                case 1:
                    successTips(data);
                    break;
            }
        })

    }

    function successTips(data){
        $(".tips").text(data.message);
     //    $(".tips")[0].style.display = "block";
         $(".tips").slideDown(0.5) 
        setInterval(() => {
             $(".tips").slideUp(0.5);
             location.href = "./login.html";
        },2000)
     //    getUserInfo(data.user.id)
    }

    function failTips(data){
         $(".tips").text(data.message);
         //    $(".tips")[0].style.display = "block";
             $(".tips").slideDown(0.5) 
         setInterval(() => {
                 $(".tips").slideUp(0.5);
         },2000)
    }
    $(".btn").on("click",register);

    function keyDown(e){
        // 获取键盘事件对象
        e = e || event;
        // 获取键盘对应值
        let $code = e.keyCode || e.which;
        if($code === 13){
            register();
        }else{
            return;
        }      
    }
    $(document).on("keydown",keyDown);


})